package com.oren.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: wangshiwei
 * Date: 2018/6/13
 * Time: 15:57
 * describe: 配置文件工具类
 */
public class PropertiesUtil {

    //获取日志文件对象
    private static final Logger LOGGER= LoggerFactory.getLogger(PropertiesUtil.class);


    /**
     * 加载配置文件
     * @param fileName 文件名称
     * @return
     */
    public static Properties loadPros(String fileName){
       Properties properties=null;
        InputStream is=null;

        try {
            //通过线程和类加载器对classPath下面的文件名进行加载  转化为流
           is=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if(is==null){
                throw new FileNotFoundException(fileName+"file not found");
            }
            properties=new Properties();
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error("load properties file failrue",e);
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error("close inputsteam failure",e);
                    e.printStackTrace();
                }
            }

        }
        return properties;
    }

    /**
     * 获取文件里面的属性值
     * @param properties
     * @param key
     * @return
     */
    public static String getString(Properties properties,String key,String...object){
        String value="";
        if(properties.containsKey(key)){
            value=properties.getProperty(key);
            if(StringUtil.isEmpty(value)){
                value=object.toString();
            }
        }
        return value;
    }


}
